package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.Article;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Controller-新闻，文章，问题
 * Created by Administrator on 2015/8/7.
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    ArticleService articleService;

    /**
     * 校园新闻头条
     */
    @ResponseBody
    @RequestMapping(value = "/top", method = RequestMethod.GET)
    public Object top() {
        List<Order> orders=new ArrayList<Order>();
        orders.add(Order.desc("id"));
        orders.add(Order.desc("isTop"));
        final List<Filter> filterList=new ArrayList<Filter>();
        filterList.add(Filter.eq("isPublication", Boolean.TRUE));
        final List<Article> list =articleService.getMListData(0,5,filterList,orders);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
           put("list", list);
       }});
    }


    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list(int pageNum,int type) {
        PageView pageView=new PageView(pageNum,10);
        pageView.addFilter(Filter.eq("isPublication",Boolean.TRUE));
        pageView.addOrder(Order.desc("id"));
        final Page page=articleService.getScrollData(pageView);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
            put("list", page.getContent());
        }});
    }

    /**
     * 详情
     */
    @ResponseBody
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public Object view(@PathVariable final Long id) {
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
            put("entity", articleService.find(id));
        }});
    }


    /**
     * 详情
     */
    @RequestMapping(value = "/viewHtml/{id}", method = RequestMethod.GET)
    public String viewHtml(@PathVariable final Long id,Model model,HttpServletRequest request) {
        model.addAttribute("entity",articleService.find(id));
        return "article/viewHtml";
    }

    /**
     * 点击数
     */
    @ResponseBody
    @RequestMapping(value = "/hits/{id}", method = RequestMethod.GET)
    public Long hits(@PathVariable Long id) {
        return articleService.viewHits(id);
    }

}
